package com.tensquare.sms.listener;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.SmsUtil;

import java.util.Map;

/**
 * 短信监听类
 */
@Component
@RabbitListener(queues = "sms")
@Slf4j
public class SmsListener {
    /**
     * 发送短信
     *
     * @param message
     */
    @Autowired
    private SmsUtil smsUtil;

    // 短信签名-可在短信控制台中找到
    private String signName = "哈哈哈哈1";
    //短信模板-可在短信控制台中找到
    private String templateCode = "SMS_151230714";

    @RabbitHandler
    public void sendSms(Map<String, String> message) throws ClientException {
        log.info("手机号：" + message.get("mobile"));
        log.info("验证码：" + message.get("numeric"));
        /**
         * 发送短信
         *  phoneNumbers 手机号
         *  signName     短信签名-可在短信控制台中找到
         *  templateCode 短信模板-可在短信控制台中找到
         *  code         验证码内容
         */
        SendSmsResponse response = smsUtil.sendSms(message.get("mobile"), signName, templateCode, message.get("numeric"));
        log.info("短信接口返回的数据----------------");
        log.info("Code=" + response.getCode());
        log.info("Message=" + response.getMessage());
        log.info("RequestId=" + response.getRequestId());
        log.info("BizId=" + response.getBizId());
    }
}
